/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.register;

import android.text.TextUtils;

import com.alstudio.module.xmpp.protocol.xmpp.data.ALXmppData;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALXmppDomainConfig;
import com.alstudio.module.xmpp.protocol.xmpp.packact.IQNameSpace;
import com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.AlXmlTag;
import com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.BaseIQTemplate;
import com.alstudio.utils.encode.ALDES;

/**
 * @author alonso lee
 * 
 */
public class RegisterIQCreator extends BaseIQTemplate {

	private ALXmppData data;

	/**
	 * 
	 */
	public RegisterIQCreator(ALXmppData data) {
		// TODO Auto-generated constructor stub
		this.data = data;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.BaseIQTemplate
	 * #getIQXml()
	 */
	@Override
	public String getIQXml() {
		// TODO Auto-generated method stub
		to = ALXmppDomainConfig.getALXmppDomainConfig().getDomainByNameSpace(
				IQNameSpace.NS_REGISTER);
		mALIQCreator.setTo(to + data.getDomain());
		mALIQCreator.setNs(IQNameSpace.NS_REGISTER);
		mALIQCreator.setType(IQ_TYPE_GET);

		// imei
		if (!TextUtils.isEmpty(data.getImei())) {
			AlXmlTag uniqueidTag = new AlXmlTag("uniqueid");
			try {
				uniqueidTag.setText(ALDES.encryptDES(data.getImei(),
						ALXmppData.DES_KEY));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tagList.add(uniqueidTag);
		}

		if (!TextUtils.isEmpty(data.getUuid())) {
			AlXmlTag uuidTag = new AlXmlTag("uuid");
			try {
				uuidTag.setText(ALDES.encryptDES(data.getUuid(),
						ALXmppData.DES_KEY));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tagList.add(uuidTag);
		}

		String thirdPartType = null;
		switch (data.getThirdPartType()) {
		case ALXmppData.LOGIN_TYPE_FACEBOOK:
			thirdPartType = "facebook";
			break;
		case ALXmppData.LOGIN_TYPE_TWITTER:
			thirdPartType = "twitter";
			break;
		case ALXmppData.LOGIN_TYPE_SINA:
			thirdPartType = "sina_id";
			break;
		case ALXmppData.LOGIN_TYPE_TENCENT:
			thirdPartType = "qq_id";
			break;
		}

		if (thirdPartType != null && !TextUtils.isEmpty(data.getThirdPartUid())) {
			AlXmlTag typeTag = new AlXmlTag(thirdPartType);
			try {
				typeTag.setText(ALDES.encryptDES(data.getThirdPartUid(),
						ALXmppData.DES_KEY));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tagList.add(typeTag);
		} else {

			AlXmlTag emailTag = new AlXmlTag("email");
			try {
				emailTag.setText(ALDES.encryptDES(data.getEmail(),
						ALXmppData.DES_KEY));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tagList.add(emailTag);

		}

		AlXmlTag passwordTag = new AlXmlTag("password");
		try {
			passwordTag.setText(ALDES.encryptDES(data.getPassword(),
					ALXmppData.DES_KEY));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 版本号
		AlXmlTag verTag = new AlXmlTag("version");
		verTag.setText(data.getAppVersion());
		// 下载连接
		AlXmlTag downTag = new AlXmlTag("downfrom");
		downTag.setText(data.getDownloadUrl());

		// 性别
		AlXmlTag sexTag = new AlXmlTag("sex");
		sexTag.setText(data.getMyRegisterInfo().getSex());

		// 昵称
		AlXmlTag nickTag = new AlXmlTag("nick");
		nickTag.setText(data.getMyRegisterInfo().getNick());

		// 生日
		AlXmlTag birthdayTag = new AlXmlTag("birthday");
		birthdayTag.setText(data.getMyRegisterInfo().getBirtyday());

		// 大图
		AlXmlTag largeavatarTag = new AlXmlTag("largeavatar");
		largeavatarTag.setText(data.getMyRegisterInfo().getAvatar());

		// 小兔
		AlXmlTag smallavatarTag = new AlXmlTag("smallavatar");
		smallavatarTag.setText(data.getMyRegisterInfo().getThumbAvatar());

		// os
		AlXmlTag osTag = new AlXmlTag("os");
		osTag.setText("android");

		// model
		AlXmlTag modelTag = new AlXmlTag("model");
		modelTag.setText(data.getDevInfo().getDeviceModel());

		// sdkversion
		AlXmlTag sdkversionTag = new AlXmlTag("sdkversion");
		sdkversionTag.setText(data.getDevInfo().getSdkVersion());

		tagList.add(sdkversionTag);
		tagList.add(modelTag);
		tagList.add(osTag);
		tagList.add(passwordTag);
		tagList.add(verTag);
		tagList.add(downTag);
		tagList.add(sexTag);
		tagList.add(nickTag);
		tagList.add(birthdayTag);
		tagList.add(largeavatarTag);
		tagList.add(smallavatarTag);
		mALIQCreator.setTagList(tagList);

		String xml = "";
		try {
			xml = mALIQCreator.getXml();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return xml;
	}

}
